package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.GoEnterpriseOrgAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.base.util.LocalAttrUtils;
import com.ztesoft.bss.common.util.SpringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Classname LocalInstallAddressAttrLinkHandler
 * @Description 获取 省市县 组合组件的值，逗号分隔的字符串，分隔后回填到 省、市、县 对应的各个属性中去
 * @Author zhang.song
 * @Date 2021-03-02 14:53
 */
@Service
@AttrHandler(GoEnterpriseOrgAttrHandler.class)
public class GoEnterpriseOrgAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Resource
    private OperateInstAttrService operateInstAttrService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Resource
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    private IOperateSceneInstService operateSceneInstService;
    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        //        业务变更初始化不校验
        String BUSI_CHANGE_SCENE_ONE = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "BUSI_CHANGE_SCENE_ONE");
        if (null!=BUSI_CHANGE_SCENE_ONE&&KiteStringUtils.isEqual(BUSI_CHANGE_SCENE_ONE, LocalKeyConsts.IFTRUE_T)) {
            return;
        }
        // 是否为页面初始化渲染，是（T），跳过校验

        String isInitPageRenderFlag = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "isInitPageRenderFlag");
        if (null!=isInitPageRenderFlag&&KiteStringUtils.isEqual(isInitPageRenderFlag, LocalKeyConsts.IFTRUE_T)) {
            return;
        }
        String sceneInstId = data.getSceneInstId();
        KiteAttr linked = new KiteAttr();
        //处理网格联动
        if(Objects.nonNull(attr.getValue())&& StringUtils.isNotBlank(attr.getValue())){
            KiteAttrValue value=  attr.getAttrValues().stream().filter(attrValue -> attr.getValue().equals(attrValue.getAttrValue())).findFirst().orElse(null);
            if(null==value){
                return;
            }
            if(KiteStringUtils.equalsAny(attr.getFieldName(),"200001378","200002595")){
                linked =inst.getAttr("200002594");
                linked.setValue(value.getAttrValueDesc());
                linked.setValueDesc(value.getAttrValueDesc());
            }else if(KiteStringUtils.equalsAny(attr.getFieldName(),"210008156","200002603")){
                linked =inst.getAttr("200002602");
                linked.setValue(value.getAttrValueDesc());
                linked.setValueDesc(value.getAttrValueDesc());
            }else if(KiteStringUtils.equalsAny(attr.getFieldName(),"210008158","200002605")){
                linked =inst.getAttr("200002604");
                linked.setValue(value.getAttrValueDesc());
                linked.setValueDesc(value.getAttrValueDesc());
            }
        }else{
            if(KiteStringUtils.equalsAny(attr.getFieldName(),"200001378","200002595")){
                linked =inst.getAttr("200002594");
                linked.setValue(StringUtils.EMPTY);
                linked.setValueDesc(StringUtils.EMPTY);
            }else if(KiteStringUtils.equalsAny(attr.getFieldName(),"210008156","200002603")){
                linked =inst.getAttr("200002602");
                linked.setValue(StringUtils.EMPTY);
                linked.setValueDesc(StringUtils.EMPTY);
            }else if(KiteStringUtils.equalsAny(attr.getFieldName(),"210008158","200002605")){
                linked =inst.getAttr("200002604");
                linked.setValue(StringUtils.EMPTY);
                linked.setValueDesc(StringUtils.EMPTY);

            }
        }
        operateSceneInstService.setAttr(sceneInstId, inst.getInstType(), inst.getInstId(), linked);
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
